﻿using System;
using System.Drawing;
using System.IO;
using System.Media;
using System.Windows.Forms;

namespace TimeTools {
    public partial class AlarmClockControl : UserControl {
        private bool _alarmSounding;
        private readonly SoundPlayer _soundPlayer = new SoundPlayer();

        public AlarmClockControl() {
            InitializeComponent();
            _soundLocation.Text = "alarm_clock_1.wav";
        }

        private void _setNow_Click(object sender, EventArgs e) {
            _enabled.Checked = false;
            _time.Text = TimeFormatHelpers.FormatDateTime(DateTime.Now);
        }

        private void _setPlus10Minutes_Click(object sender, EventArgs e) {
            try {
                _enabled.Checked = false;
                DateTime t = DateTime.Parse(_time.Text);
                t += new TimeSpan(0, 0, 10, 0);
                _time.Text = TimeFormatHelpers.FormatDateTime(t);
                _time.ForeColor = Color.Black;
            } catch {
                _time.ForeColor = Color.Red;
            }
        }

        private void _setPlus1Hour_Click(object sender, EventArgs e) {
            try {
                _enabled.Checked = false;
                DateTime t = DateTime.Parse(_time.Text);
                t += new TimeSpan(0, 1, 0, 0);
                _time.Text = TimeFormatHelpers.FormatDateTime(t);
                _time.ForeColor = Color.Black;
            } catch {
                _time.ForeColor = Color.Red;
            }
        }

        private void _time_TextChanged(object sender, EventArgs e) {
            try {
                _enabled.Checked = false;
                DateTime.Parse(_time.Text);
                _time.ForeColor = Color.Black;
            } catch {
                _time.ForeColor = Color.Red;
            }
        }

        private void _alarmTimer_Tick(object sender, EventArgs e) {
            _currentTime.Text = TimeFormatHelpers.FormatDateTimeWithoutMillis(DateTime.Now);
            if (_enabled.Checked && !_alarmSounding) {
                DateTime t;
                if (!DateTime.TryParse(_time.Text, out t)) {
                    _time.ForeColor = Color.Red;
                    return;
                }
                if (DateTime.Now > t) {
                    _alarmSounding = true;
                    _snooze.Enabled = true;
                    _snooze.ForeColor = SystemColors.ControlText;
                    _off.Enabled = true;
                    _off.ForeColor = SystemColors.ControlText;
                    _soundPlayer.PlayLooping();
                    AlarmMessageBox alarmMessageBox = new AlarmMessageBox(this);
                    alarmMessageBox.Show(this);
                }
            }
        }

        private void _snooze_Click(object sender, EventArgs e) {
            Snooze();
        }

        public void Snooze() {
            if (_alarmSounding) {
                _alarmSounding = false;
                _soundPlayer.Stop();

                try {
                    DateTime t = DateTime.Parse(_time.Text);
                    t += new TimeSpan(0, 0, 10, 0);
                    _time.Text = TimeFormatHelpers.FormatDateTime(t);
                    _snooze.Enabled = false;
                    _snooze.ForeColor = SystemColors.ControlText;
                    _enabled.Checked = true;
                    _time.ForeColor = Color.Black;
                } catch {
                    _time.ForeColor = Color.Red;
                }
            }
        }

        private void _off_Click(object sender, EventArgs e) {
            Off();
        }

        public void Off() {
            _alarmSounding = false;
            _soundPlayer.Stop();
            _enabled.Checked = false;
            _snooze.Enabled = false;
            _snooze.ForeColor = SystemColors.ControlText;
            _off.Enabled = false;
            _off.ForeColor = SystemColors.ControlText;
        }

        private void _soundLocation_TextChanged(object sender, EventArgs e) {
            try {
                if (File.Exists(_soundLocation.Text)) {
                    _soundPlayer.SoundLocation = _soundLocation.Text;
                    _soundLocation.ForeColor = Color.Black;
                } else {
                    _soundLocation.ForeColor = Color.Red;
                }
            } catch {
                _soundLocation.ForeColor = Color.Red;
            }
        }

        private void _soundLocationBrowse_Click(object sender, EventArgs e) {
            OpenFileDialog dlg = new OpenFileDialog {
                Filter = "Sound Files (*.wav)|*.wav|All Files (*.*)|*.*"
            };
            DialogResult result = dlg.ShowDialog(this);
            if (result == DialogResult.OK) {
                _soundLocation.Text = dlg.FileName;
            }
        }
    }
}
